<?php
/**
 * Created by PhpStorm.
 * User: 86182
 * Date: 2020/6/26
 * Time: 13:31
 */

namespace App\Serves;

use App\Models\Goods\Category as CategoryModel;
use App\Models\Goods\Category;


class CategoryServe
{
    public static function getAllCategory(){
        $category = CategoryModel::getAllCategory();
        return $category ?? [] ;
    }

    public static function add($data){
        $category = new CategoryModel();
        try{
            $category->pid = $data['pid'];
            $category->name = $data['name'];
            $category->status = $data['status'];
            $category->weigh = $data['weigh'];
            $res = $category->save($data);
        }catch (\Exception $e){
            return false;
        }

        if($res) return true;
    }

    public static function save($data){
        $category = CategoryModel::find($data['id']);
        try{
            $category->pid = $data['pid'];
            $category->name = $data['name'];
            $category->status = $data['status'];
            $category->weigh = $data['weigh'];
            $res = $category->save($data);
        }catch (\Exception $e){
            return false;
        }

        if($res) return true;
    }

    public static function del($id){
        try{
            $res = CategoryModel::destroy($id);
        }catch (\Exception $e){
            return false;
        }

        if($res) return true;
    }


}